<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>答题提交测试</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>
    <h1>答题提交测试</h1>

    <div>
        <h3>测试数据</h3>
        <p>用户ID: <input type="number" id="userId" value="1"></p>
        <p>题目ID: <input type="number" id="questionId" value="1"></p>
        <p>答案: <input type="text" id="answerContent" value="A" placeholder="输入答案"></p>
        <button onclick="submitAnswer()">提交答案</button>
    </div>

    <div>
        <h3>响应结果</h3>
        <pre id="result"></pre>
    </div>

    <div>
        <h3>获取随机题目</h3>
        <button onclick="getRandomQuestion()">获取题目</button>
        <pre id="questionResult"></pre>
    </div>

    <script>
        const baseURL = 'http://localhost:8080';

        async function submitAnswer() {
            const data = {
                userId: parseInt(document.getElementById('userId').value),
                questionId: parseInt(document.getElementById('questionId').value),
                answerContent: document.getElementById('answerContent').value
            };

            console.log('提交数据:', data);

            try {
                const response = await axios.post(`${baseURL}/userAnswer/submitAnswer`, data);
                document.getElementById('result').textContent = JSON.stringify(response.data, null, 2);
                console.log('响应:', response.data);
            } catch (error) {
                document.getElementById('result').textContent = '错误: ' + error.message;
                console.error('错误:', error);
            }
        }

        async function getRandomQuestion() {
            try {
                const response = await axios.get(`${baseURL}/question/getRandomQuestion`);
                document.getElementById('questionResult').textContent = JSON.stringify(response.data, null, 2);

                // 自动填充题目ID
                if (response.data.data && response.data.data.questionId) {
                    document.getElementById('questionId').value = response.data.data.questionId;
                }
                console.log('题目:', response.data);
            } catch (error) {
                document.getElementById('questionResult').textContent = '错误: ' + error.message;
                console.error('错误:', error);
            }
        }
    </script>
</body>

</html>